package com.rfid.spec.pc301;
import com.rfid.api.GFunction;
import com.rfid.reader.DriverPC301;
import com.rfid.spec.L0_APL_ISO15693;
import com.rfid.spec.L0_ISO15693_PICC_HANDLE;
import com.rfid.spec.L0_PICC;
import com.rfid.spec.SpecAIPInvenParamISO15693;
import com.rfid.spec.SpecAirProtocInvenParam;
import com.rfid.spec.SpecConnPrmsISO15693;
import com.rfid.spec.SpecConnectParams;
import com.rfid.spec.SpecInvenParamSet;
import com.rfid.spec.TagOperateHandle;

public class AN_PC301_APL_ISO15693 extends L0_APL_ISO15693 {
    public static AN_PC301_APL_ISO15693 create() {
        return new AN_PC301_APL_ISO15693();
    }

    public int Inventory(byte AIType, int mTimeout, SpecAirProtocInvenParam AIPInvnParm, SpecInvenParamSet otherParams) {
        SpecAIPInvenParamISO15693 pInvnParam;
        DriverPC301 prdr = (DriverPC301) this.m_reader;
        int iret = 0;
        prdr.m_syncLock.lock();
        prdr.pbSnd.flush();
        if (AIPInvnParm instanceof SpecAIPInvenParamISO15693)
            pInvnParam = (SpecAIPInvenParamISO15693) AIPInvnParm;
        else {
            prdr.m_syncLock.unlock();
            return -3;
        }
        byte InvenMode = 0;
        if (AIType != 1 && AIType == 2) InvenMode = (byte) (InvenMode | 0x1);
        if (pInvnParam.m_enableAFI) InvenMode = (byte) (InvenMode | 0x2);
        char cmd=510;
        prdr.pbSnd.WriteData(cmd);
        prdr.pbSnd.WriteData(InvenMode);
        if (pInvnParam.m_enableAFI) {
            prdr.pbSnd.WriteData((byte) 1);
            prdr.pbSnd.WriteData(pInvnParam.m_AFI);
        }
        iret = prdr.InventoryTransceive(prdr.pbSnd, mTimeout);
        prdr.m_syncLock.unlock();
        return iret;
    }

    public int Connect(L0_PICC picc, SpecConnectParams Params, TagOperateHandle hOut) {
        byte[] uid = new byte[8];
        SpecConnPrmsISO15693 iso15693Prm = (SpecConnPrmsISO15693) Params;
        if (iso15693Prm == null) return -3;
        if (iso15693Prm.m_AddrMode == 1) {
            System.arraycopy(iso15693Prm.m_UID, 0, uid, 0, uid.length);
            GFunction.BytesReverse(uid, uid.length);
        } else if (iso15693Prm.m_AddrMode != 0) if (iso15693Prm.m_AddrMode == 2) {
            System.arraycopy(iso15693Prm.m_UID, 0, uid, 0, uid.length);
            GFunction.BytesReverse(uid, uid.length);
            int iret = CmdSelect(uid);
            if (iret != 0) return iret;
        } else return -4;
        L0_ISO15693_PICC_HANDLE h = L0_ISO15693_PICC_HANDLE.Create();
        if (h != null) {
            h.m_picc = picc;
            h.m_AddressMode = iso15693Prm.m_AddrMode;
            System.arraycopy(uid, 0, h.m_uid, 0, 8);
        }
        hOut.setTagHandle(h);
        return 0;
    }

    public int CmdReset() {
        DriverPC301 pT = (DriverPC301) this.m_reader;
        int iret = 0;
        pT.m_syncLock.lock();
        pT.pbSnd.flush();
        pT.pbRecv.flush();
        char cmd=11262;
        pT.pbSnd.WriteData(cmd);
        pT.pbSnd.WriteData((byte) 0);
        iret = pT.transceive(cmd, pT.pbSnd, pT.pbRecv, 500L, false);
        pT.m_syncLock.unlock();
        return iret;
    }

    private int CmdSelect(byte[] uid) {
        int iret = -1;
        DriverPC301 pT = (DriverPC301) this.m_reader;
        pT.m_syncLock.lock();
        pT.pbSnd.flush();
        pT.pbRecv.flush();
        char cmd=9726;
        pT.pbSnd.WriteData(cmd);
        pT.pbSnd.WriteData((byte) 1);
        pT.pbSnd.WriteData(uid, 0, 8);
        iret = pT.transceive(cmd, pT.pbSnd, pT.pbRecv, 500L, false);
        pT.m_syncLock.unlock();
        if (iret != 0) return iret;
        return 0;
    }
}
